var categories = [] //产品类别
var insuranceCategories = [] //险种类别
var types = [] //发布栏目
var permissionses = [] //可查看人员
var organs = [] //可查看机构
var productFilesTypes = [] //产品属性附件类别
var activityFilesTypes = [] //项目/活动附件类别
var saleFilesTypes = [] //销售计划附件类别
var products = [] //使用产品
var channels = [] //使用渠道
var productState = [] //产品状态
var relaseState = [] //发布状态
new Vue({
    el: '#mainBody',
    data: function () {
        return {
            productSearchBar: false, //展开产品搜索条件
            activitySearchBar: false, //展开活动搜索条件
            showDialog: false, //审批dialog
            showProductDialog:false,
            showActivityDialog:false,
            showSaleDialog:false,
            productSearchForm: { //产品查询条件
            	ident:'2',
            	productcode: '',
            	productname: '',
            	atttype: '',
            	createtime: '',
            	productstate: '',
            	releasestate: ''
            },
            activitySearchForm: { //活动查询条件
            	ident:'1',
            	projectname: '',
            	projectstartdate: '',
            	releasestate: '',
            	createname: '',
            	createtime: '',
            	atttype: ''
            },
            productSearchForm2 : { // 查询条件
            	ident:'2',
            	productcode: '',
            	productname: '',
            	atttype: '',
            	createtime: '',
            	productstate: '',
            	releasestate: ''
			},
			activitySearchForm2 : { // 查询条件
				ident:'1',
            	projectname: '',
            	projectstartdate: '',
            	releasestate: '',
            	createname: '',
            	createtime: '',
            	atttype: ''
			},
			start1 : false,
			start2 : false,
			start3 : false,
			start4 : false,
            approveRadio: '审批通过', //审批是否通过
            approveNoNotes: false, //审批未通过原因
            approveType: '1', //1:产品审批2:活动审批
            approveData: [], //产品or活动审批的id集
            approveNotes:'',//审批原因
            categories: [ //产品类别
            ],
            accessoriesaCategories: [ //附件类别
            ],
            activityFilesTypes: [] ,//附件类别
            form: {}, //审批的数据
            isDisabled: true, //disabled
            productItems: [ //产品资料列表
            ],
            activityItems: [ //项目活动列表
            ],
            productState: [ //产品状态
            ],
            relaseState: [ //发布状态
            ],
            currentpage:1,//当前页数
            pageBean:[//分页参数列表
            ],
            productcurrentpage:1,//当前页数
            productpageBean:[//分页参数列表
            ]
            
        };
    },
    created: function () {
    	this.loadOption();
    	this.findproject();
    	this.findproduct();
    },
    methods: {
    	showProduct: function (index) { //查看
    		console.log(this.productItems[index])
    		if(this.productItems[index].ident=='0'){
    			this.showProductDialog = true
    		}else{
    			this.showSaleDialog = true
    		}
            this.isDisabled = true
            this.form = this.productItems[index]
        },
        showActivity: function (index) { //查看
        	 this.showActivityDialog = true
             this.isDisabled = true
             this.form = this.activityItems[index]
        },
        productApprove: function (index) { //产品资料审批
        	this.approveRadio = '审批通过'
        	this.approveNoNotes = false
        	this.approveType= '1'; 
        	this.approveNotes='';
        	this.approveData= new Array();
    		this.approveData.push(this.productItems[index].id);
            this.showDialog = true
        },
        batchProductApprove: function () { //产品资料批量审批
        	this.approveRadio = '审批通过'
        	this.approveNoNotes = false
            if (this.productCheckedCount !== 0) {
            	this.approveType= '1'; 
            	this.approveNotes='';
        		this.approveData= new Array();
            	for(var i=0;i<this.productItems.length;i++){
            		if(this.productItems[i].checked==true){
            			this.approveData.push(this.productItems[i].id);
            		}
            	}
                this.showDialog = true
            } else {
                warningMessage(this.$message,'请先勾选需要审批的列表')
            }
        },
        activityApprove: function (index) { //项目/活动发布审批
        	this.approveRadio = '审批通过'
        	this.approveNoNotes = false
        	this.approveType= '2'; 
        	this.approveNotes='';
        	this.approveData= new Array();
    		this.approveData.push(this.activityItems[index].id);
            this.showDialog = true
        },
        batchActivityApprove: function () { //项目/活动批量审批
        	this.approveRadio = '审批通过'
        	this.approveNoNotes = false
            if (this.activityCheckedCount !== 0) {
            	this.approveType= '2';
            	this.approveNotes='';
        		this.approveData= new Array();
            	for(var i=0;i<this.activityItems.length;i++){
            		if(this.activityItems[i].checked==true){
            			this.approveData.push(this.activityItems[i].id);
            		}
            	}
                this.showDialog = true
            } else {
                warningMessage(this.$message,'请先勾选需要审批的列表')
            }
        },
        /**
         * 页面初始化,加载下拉选项
         * */
        loadOption: function (){
        	var _this = this;
            $.ajax({
                url: '/product/loadOption',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                	//合并两个数组，去重  
                	var concat = _this.concat(res.data.productEnclosureCategory,res.data.saleEnclosureCategory);
                	_this.accessoriesaCategories = concat;//产品属性附件类别
                	_this.activityFilesTypes = res.data.projectEnclosureCategory;//活动附件类别
                	_this.productState = res.data.productState;//产品状态
                	_this.relaseState = res.data.releaseState;//发布状态
                 },
                error: function (err) {
                	if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
                	errorMessage(_this.$message, '初始化下拉列表出错！')
                }
            })
        },
        concat: function(arr1,arr2){  
    	    //不要直接使用var arr = arr1，这样arr只是arr1的一个引用，两者的修改会互相影响  
    	    var arr = arr1.concat();  
    	    //或者使用slice()复制，var arr = arr1.slice(0)  
    	    for(var i=0;i<arr2.length;i++){  
    	        arr.indexOf(arr2[i]) === -1 ? arr.push(arr2[i]) : 0;  
    	    }  
    	    return arr;  
    	},
        saveForm: function () {//保存审批
            var _this = this;
            _this.showDialog = false;
        	var url='';
        	if(this.approveType=='1'){
        		url='/product/ApprovalProduct'
        	}else{
        		url='/project/ApprovalProject'
        	}
        	var data= new Object();
        	data.approveRadio = this.approveRadio;
        	data.approveData = this.approveData;
        	data.approveNotes = this.approveNotes;
        	$.ajax({
                url: url,
                data: {"data":JSON.stringify(data)},
                type: 'post',
                dataType: 'text',
                success: function (res) {
                	var length = sessionStorage.getItem('length');
                	var activityLength = sessionStorage.getItem('activityLength');
                	if(_this.approveType=='1'){
                		if(length == _this.approveData.length){
                			if(_this.productcurrentpage == _this.productpageBean.totalPage){
                				_this.productcurrentpage = _this.productcurrentpage-1;
                			}	
                		}
                	}else{
	            		if(activityLength == _this.approveData.length){
	            			if(_this.currentpage == _this.pageBean.totalPage){
	            				_this.currentpage = _this.currentpage-1;
	            			}	
	            		}
                	}
                     successMessage(_this.$message,'恭喜您，完成审批')
                	_this.findproduct();
                	_this.findproject();
                },
                error: function (err) {
                    if(err.status == 403){
                        errorMessage(_this.$message, "没有权限，请先申请该权限！");
                        return;
                    }
                	if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }

                    errorMessage(_this.$message,err);
                }
            })
        },
        resetChecked: function () {//处理页面钩子
        	 for (var i = 0; i < this.productItems.length; i++) {
                 this.$set(this.productItems[i], 'checked', false)
             }
             for (var i = 0; i < this.activityItems.length; i++) {
                 this.$set(this.activityItems[i], 'checked', false)
             }
        },
        changeRadio: function () { //审批通过or不通过
    		if (this.approveRadio === '审批未通过') {
    			this.approveNoNotes = true
    		} else {
    			this.approveNoNotes = false
    		}
        },
        resetSearchForm: function (formName) { //清空搜索条件
            this.$refs[formName].resetFields()
            if(formName=="activitySearchForm"){
            	this.start1 = false;
    			this.start2 = true;
    			this.findproject();
            }else if(formName=="productSearchForm"){
            	this.start3 = false;
    			this.start4 = true;
    			this.findproduct();
            }
        },
        /**
         * 下一页上一页按钮
         * */
        producthandleCurrentChange:function(val){
        	this.productcurrentpage=val;
        	if (this.start3) {// false表示没点击搜索
				var data = JSON.stringify(this.productSearchForm);
			} else {
				var data = JSON.stringify(this.productSearchForm2);
			}
        	this.findproduct(data);
        },
        /**
    	 * 下一页上一页按钮
    	 * */
    	handleCurrentChange:function(val){
    		this.currentpage=val;
    		if (this.start1) {// false表示没点击搜索
				var data = JSON.stringify(this.activitySearchForm);
			} else {
				var data = JSON.stringify(this.activitySearchForm2);
			}
    		this.findproject(data);
    	},
    	findproject:function (data) {
            var _this = this;
            if (!data) {
            	if (this.start1) {// false表示没点击搜索
    				var data = JSON.stringify(this.activitySearchForm);
    			} else {
    				var data = JSON.stringify(this.activitySearchForm2);
    			}
			}
   	       $.ajax({
   	          url: '/project/findProject',
   	          type: 'post',
   	          data:{"data":data,"page":_this.currentpage},
   	          dataType: 'json',
   	          success: function (res) {
   	        	_this.activityItems = res.data.beanList;
   	        	sessionStorage.setItem('activityLength',res.data.beanList.length);
   	        	var page = {}
                page['totalNumber'] = res.data.totalNumber
                page['totalPage'] = res.data.totalPage
                _this.pageBean = page;
                _this.resetChecked();
   	          },
   	          error: function (err) {
   	        	if(err.responseJSON.message=="session clean"){
                    errorMessage(_this.$message, '登陆已失效重新登陆!');
                    toLogin();
                    return;
                }
                if(err.status == 403){
                    errorMessage(_this.$message, "没有权限，请先申请该权限！");
                    return;
   	        	}
   	        	errorMessage(_this.$message, '获取项目/活动列表出错！')
   	          }
   	      })
       	},
       	findproduct:function (data) {//产品查询
            var _this = this;
            if (!data) {
            	if (this.start3) {// false表示没点击搜索
    				var data = JSON.stringify(this.productSearchForm);
    			} else {
    				var data = JSON.stringify(this.productSearchForm2);
    			}
			}
   	       $.ajax({
   	          url: '/product/findProduct',
   	          type: 'post',
   	          data:{"data":data,"page":_this.productcurrentpage},
   	          dataType: 'json',
   	          success: function (res) {
   	        	_this.productItems = res.data.beanList;
   	        	sessionStorage.setItem('length',res.data.beanList.length);
   	        	var page = {}
   				page['totalNumber'] = res.data.totalNumber
   				page['totalPage'] = res.data.totalPage
   				_this.productpageBean = page
   				_this.resetChecked();
   	          },
   	          error: function (err) {
		        	if(err.responseJSON.message=="session clean"){
		                errorMessage(_this.$message, '登陆已失效重新登陆!');
		                toLogin();
		                return;
		            }
                  if(err.status == 403){
                      errorMessage(_this.$message, "没有权限，请先申请该权限！");
                      return;
                  }
   	        	  errorMessage(_this.$message, '获取产品组合列表出错！')
   	          }
   	      })
       	},
       	/**
         * 多条件查询
         * */
        selectProduct:function () {
        	this.productcurrentpage = 1;
        	if(this.productSearchForm.productcode=="" && this.productSearchForm.productname=="" && this.productSearchForm.atttype=="" && this.productSearchForm.createtime=="" && this.productSearchForm.productstate=="" && this.productSearchForm.releasestate==""){
	        	if (this.start4) {
					this.start3 = false;
					this.start4 = false;
	        	}
			} else {
				this.start3 = true;
			}
        	this.findproduct();
        },
        /**
         * 多条件查询
         * */
        selectProject:function () {
        	this.currentpage = 1;
        	if(this.activitySearchForm.projectname=="" && this.activitySearchForm.projectstartdate=="" && this.activitySearchForm.releasestate=="" && this.activitySearchForm.createname=="" && this.activitySearchForm.createtime=="" && this.activitySearchForm.atttype==""){
        		if (this.start2) {
					this.start1 = false;
					this.start2 = false;
	        	}
			} else {
				this.start1 = true;
			}
        	this.findproject();
        },
        //下载附件
        downloadFile :function(file){
        	window.location.href='/product/downloadFile?data='+encodeURIComponent(JSON.stringify(file));
        },
//        defaultSelection : function(){//默认选中
//			var Main = {
//			    data () {
//			      return {
//			    	  approveRadio: '审批通过'
//			      };
//			    }
//			  }
//			var Ctor = Vue.extend(Main)
//			new Ctor().$mount('#approveRadioWraps')
//		}
    },
    computed: {
        productCheckAll: {
            get: function () {
                return this.productCheckedCount === this.productItems.length
            },
            set: function (value) {
                this.productItems = this.productItems.map(function (item) {
                    item.checked = value
                    return item
                })
            }
        },
        productCheckedCount: {
            get: function () {
                return this.productItems.filter(function (item) {
                    return item.checked
                }).length
            }
        },
        activityCheckAll: {
            get: function () {
                return this.activityCheckedCount === this.activityItems.length
            },
            set: function (value) {
                this.activityItems = this.activityItems.map(function (item) {
                    item.checked = value
                    return item
                })
            }
        },
        activityCheckedCount: {
            get: function () {
                return this.activityItems.filter(function (item) {
                    return item.checked
                }).length
            }
        }
    }
})


//$(function () {
//    $('.pcoded-left-item').children('li').siblings().removeClass('active pcoded-trigger')
//    $('#4').addClass('active pcoded-trigger')
//    $('#43').addClass('active')
//})